import store from '@/store'
import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '../views/Layout/index.vue'

const routes = [
  { path: '/test', component: () => import('@/views/test') },
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    children: [
      { path: '/', component: () => import('@/views/Home/index.vue') },
      { path: '/category/:id', component: () => import('@/views/category') },
      { path: '/product/:id', component: () => import('@/views/Goods/index.vue') }
    ]
  },
  { path: '/login', component: () => import('@/views/Login/index.vue') },
  { path: '/login/callback', component: () => import('@/views/Login/callback') }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path.startsWith('/member/order') && !store.state.user.profile.token) {
    next('/login?redirectUrl=' + to.fullPath)
  } else {
    next()
  }
})

export default router
